package com.green.system.controller;

import com.green.system.common.Constant;
import com.green.system.common.DataGridView;
import com.green.system.common.ResultObj;
import com.green.system.domain.Role;
import com.green.system.service.RoleService;
import com.green.system.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Green
 * @Date: 2021/10/24/12:22
 * @Description:
 */

@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService ;


    /**
     * 查询
     * @param roleVo
     * @return
     */
    @RequestMapping("loadAllRole")
    public Object loadAllRole(RoleVo roleVo){
        return this.roleService.queryAllRole(roleVo);
    }


    /**
     * 查询所有可用角色不分页
     * @param roleVo
     * @return
     */
    @RequestMapping("loadAllAvailableRoleNoPage")
    public Object loadAllAvailableRoleNoPage(RoleVo roleVo){
        roleVo.setAvailable(Constant.AVAILABLE_TRUE);
        return this.roleService.queryAllAvailableRoleNoPage(roleVo);
    }


    /**
     * 添加角色
     */
    @PostMapping("addRole")
    public ResultObj addRole(Role role){
        try {
            role.setCreatetime(new Date());
            role.setAvailable(Constant.AVAILABLE_TRUE);
            this.roleService.saveRole(role);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改角色
     */
    @PostMapping("updateRole")
    public ResultObj updateRole(Role role){
        try {
            this.roleService.updateRole(role);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }



    /**
     * 删除角色
     */
    @RequestMapping("deleteRole")
    public ResultObj deleteRole(Integer id){
        try {
            this.roleService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
    * @Description: 查询角色ID再查询角色拥有的菜单和权限ID
    * @Param: [id]
    * @return: com.green.system.common.DataGridView
    * @Author: Green
    * @Date: 2021/10/31
    */
    
    @RequestMapping("queryMenuIdsByRid")
    public DataGridView queryMenuIdsByRid(Integer id){
        List<Integer> mids = this.roleService.queryMenuIdsByRid(id);
        return new DataGridView(mids);
    }


    /**
     * 保存角色和菜单权限之间的关系
     */
    @RequestMapping("saveRoleMenu")
    public ResultObj saveRoleMenu(Integer rid , Integer[] mids){
        try{
            this.roleService.saveRoleMenu(rid,mids);
            return ResultObj.DISPATCH_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DISPATCH_ERROR;
        }
    }

    /**
     * 批量删除
     */
    @RequestMapping("batchDeleteRole")
    public ResultObj batchDeleteRole(Integer[] ids){
        try {
            if (null != ids && ids.length > 0){
                List<Integer> idsList = new ArrayList<>();
                for (Integer id : ids) {
                    idsList.add(id);
                }
                this.roleService.removeByIds(idsList);
                return ResultObj.DELETE_SUCCESS;
            }else{
                return new ResultObj(-1,"传入ID不能为空");
            }

        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}
